<template>
  <el-dialog v-model="addOrEditVisible" title="开卡记录"
             :before-close="closeEditDialog"
             width="50%">
    <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules"
             label-width="160px" class="demo-ruleForm"
             :size="formSize" status-icon>
<!--      <el-row>-->
<!--        <el-col :span="24">-->
<!--          <el-form-item label="" prop="id">-->
<!--            <el-input v-model="ruleForm.id"/>-->
<!--          </el-form-item>-->
<!--        </el-col>-->
<!--      </el-row>-->
      <el-row>
        <el-col :span="24">
          <el-form-item label="商户名称" prop="shopId">
<!--            <el-input v-model="ruleForm.shopId"/>-->
            <el-select v-model="ruleForm.shopId"
                       filterable
                       placeholder="请选择卡项" style="width:100%">
              <el-option v-for="item in shops" :label="item.poiName" :value=item.id></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="卡项" prop="cardType">
<!--            <el-input v-model="ruleForm.cardType"/>-->
            <el-select v-model="ruleForm.cardType" @change="cardTypeChange" placeholder="请选择卡项" style="width:100%">
              <el-option v-for="item in cards" :label="item.cardName" :value=item.id></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="卡费" prop="cardPrice">
            <el-input v-model="ruleForm.cardPrice"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="开始时间" prop="startTime">
<!--            <el-input v-model="ruleForm.startTime"/>-->
            <el-date-picker
                v-model="ruleForm.startTime"
                type="date"
                style="width: 100%"
                placeholder="选择开始时间"
                value-format="YYYY-MM-DD"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="结束时间" prop="endTime">
            <el-date-picker
                v-model="ruleForm.endTime"
                type="date"
                style="width: 100%"
                placeholder="选择结束时间"
                value-format="YYYY-MM-DD"
            />
<!--            <el-input v-model="ruleForm.endTime"/>-->
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="审核状态" prop="verifyFlag">
<!--            <el-input v-model="ruleForm.verifyFlag"/>-->
            <el-select v-model="ruleForm.verifyFlag" placeholder="请选择审核状态" style="width:160px">
              <el-option label="待审核" disabled :value=0></el-option>
              <el-option label="审核通过" :value=1></el-option>
              <el-option label="审核不通过" :value=2></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="24">
          <el-form-item label="审核备注" prop="verifyRemark">
            <el-input type="textarea" v-model="ruleForm.verifyRemark"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="审核时间" prop="verifyTime">
            {{ruleForm.verifyTime || moment().format('YYYY-MM-DD HH:mm:ss')}}
          </el-form-item>
        </el-col>
      </el-row>

<!--      <el-row>-->
<!--        <el-col :span="24">-->
<!--          <el-form-item label="状态 0-停用 1-启用 2-备用" prop="statusFlag">-->
<!--            <el-input v-model="ruleForm.statusFlag"/>-->
<!--          </el-form-item>-->
<!--        </el-col>-->
<!--      </el-row>-->
<!--      <el-row>-->
<!--        <el-col :span="24">-->
<!--          <el-form-item label="是否删除 0-否 1-是" prop="delFlag">-->
<!--            <el-input v-model="ruleForm.delFlag"/>-->
<!--          </el-form-item>-->
<!--        </el-col>-->
<!--      </el-row>-->
<!--      <el-row>-->
<!--        <el-col :span="24">-->
<!--          <el-form-item label="创建人" prop="createUser">-->
<!--            <el-input v-model="ruleForm.createUser"/>-->
<!--          </el-form-item>-->
<!--        </el-col>-->
<!--      </el-row>-->
<!--      <el-row>-->
<!--        <el-col :span="24">-->
<!--          <el-form-item label="创建时间" prop="createTime">-->
<!--            <el-input v-model="ruleForm.createTime"/>-->
<!--          </el-form-item>-->
<!--        </el-col>-->
<!--      </el-row>-->
<!--      <el-row>-->
<!--        <el-col :span="24">-->
<!--          <el-form-item label="修改时间" prop="updateTime">-->
<!--            <el-input v-model="ruleForm.updateTime"/>-->
<!--          </el-form-item>-->
<!--        </el-col>-->
<!--      </el-row>-->
<!--      <el-row>-->
<!--        <el-col :span="24">-->
<!--          <el-form-item label="备注" prop="remark">-->
<!--            <el-input v-model="ruleForm.remark"/>-->
<!--          </el-form-item>-->
<!--        </el-col>-->
<!--      </el-row>-->
    </el-form>
    <template #footer>
         <span class="dialog-footer">
           <el-button @click="closeEditDialog">关闭</el-button>
           <el-button type="primary"
                      @click="submitForm(ruleFormRef)">提交</el-button>
         </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import {reactive, ref} from 'vue'
import type {FormInstance, FormRules} from 'element-plus'
import {ElMessage} from "element-plus";
import router from "@/router";
import {ShopCardLog} from '@/apis/model/shop/ShopCardLogModel';
import {
  insertShopCardLog,
  updateShopCardLog
} from '@/apis/http/shop/ShopCardLogHttp';
import moment from "moment";
import {ShopCard} from "@/apis/model/shop/ShopCardModel";
import {RetailShop} from "@/apis/model/shop/RetailShopModel";

interface Prop {
  addOrEditVisible: boolean,
  ruleForm: ShopCardLog,
  cards: Array<ShopCard>,
  shops: Array<RetailShop>,
  addDataType: string
}

// 刷新list页面
const refushList = ref(false);

// 传递过来的参数
const props = defineProps<Prop>();

const emit = defineEmits(["closeEditDialog"]);

// 关闭按钮
const closeEditDialog = () => {
  emit("closeEditDialog", {
    addOrEditVisible: !props.addOrEditVisible,
    refushList: refushList.value
  })
}
const formSize = ref('default')
const ruleFormRef = ref<FormInstance>()

const rules = reactive<FormRules>({
  id: [
    {required: true, message: '请输入', trigger: 'blur'}
  ],
  shopId: [
    {required: true, message: '请输入商户编号', trigger: 'blur'}
  ],
  cardType: [
    {required: true, message: '请输入卡项 1-月卡 2-季卡 3-半年卡 4-年卡', trigger: 'blur'}
  ],
  cardPrice: [
    {required: true, message: '请输入卡费', trigger: 'blur'}
  ],
  startTime: [
    {required: true, message: '请输入开始时间', trigger: 'blur'}
  ],
  endTime: [
    {required: true, message: '请输入结束时间', trigger: 'blur'}
  ],
  verifyFlag: [
    {required: true, message: '请输入审核状态 0-待审核 1-审核通过 2-审核不通过', trigger: 'blur'}
  ],
  // verifyTime: [
  //   {required: true, message: '请输入审核时间', trigger: 'blur'}
  // ],
  verifyRemark: [
    {required: true, message: '请输入审核备注', trigger: 'blur'}
  ],
  statusFlag: [
    {required: true, message: '请输入状态 0-停用 1-启用 2-备用', trigger: 'blur'}
  ],
  delFlag: [
    {required: true, message: '请输入是否删除 0-否 1-是', trigger: 'blur'}
  ],
  createUser: [
    {required: true, message: '请输入创建人', trigger: 'blur'}
  ],
  createTime: [
    {required: true, message: '请输入创建时间', trigger: 'blur'}
  ],
  updateTime: [
    {required: true, message: '请输入修改时间', trigger: 'blur'}
  ],
  remark: [
    {required: true, message: '请输入备注', trigger: 'blur'}
  ],
})

const cardTypeChange = (e) =>{
  console.log(e);
}
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      // 新增
      if (props.addDataType === 'add') {
        insertShopCardLog(props.ruleForm).then(res => {
          const code = res.code;
          const message: string = res.message;
          const data = res.data;
          if (code == 200) {
            ElMessage({
              message: '创建成功！',
              type: 'success',
            })
            refushList.value = true
            closeEditDialog()
          } else {
            ElMessage.error('创建失败！')
          }
        })
      } else if (props.addDataType === 'edit') {
        updateShopCardLog(props.ruleForm).then(res => {
          const code = res.code;
          const message: string = res.message;
          const data = res.data;
          if (code == 200) {
            ElMessage({
              message: '更新成功！',
              type: 'success',
            })
            refushList.value = true
            closeEditDialog()
          } else {
            ElMessage.error('更新失败！')
          }
        })
      }

    } else {
      ElMessage.error('请完善信息！')
    }
  })
}
</script>

<style scoped>
.dialog-footer button:first-child {
  margin-right: 10px;
}
</style>
